package com.htl.exampro.course.controller;

import com.htl.exampro.base.model.ServerResponse;
import com.htl.exampro.base.utils.UserUtil;
import com.htl.exampro.course.pojo.vo.CourseSelectionVO;
import com.htl.exampro.course.pojo.vo.CourseVO;
import com.htl.exampro.course.service.CourseSelectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 用户选课关联表 前端控制器
 * </p>
 *
 * @author hehehe
 * @since 2025-05-26
 */
@RestController
@RequestMapping("/course_selection")
public class CourseSelectionController {

    @Autowired
    private CourseSelectionService courseSelectionService;

    @GetMapping("/my-course")
    public ServerResponse<List<CourseVO>> getMyCourses() {
        Long userId = Long.valueOf(UserUtil.getUserId());
        List<CourseVO> courseVOS = courseSelectionService.getCoursesByUser(userId);
        return ServerResponse.success(courseVOS);
    }

    @GetMapping("/user/{userId}")
    public ServerResponse<List<CourseVO>> getCoursesByUser(@PathVariable("userId") Long userId) {
        List<CourseVO> courseVOS = courseSelectionService.getCoursesByUser(userId);
        return ServerResponse.success(courseVOS);
    }

    @GetMapping("/course/{courseId}")
    public ServerResponse<List<CourseSelectionVO>> getUsersByCourse(@PathVariable("courseId") Long courseId) {
        List<CourseSelectionVO> courseSelectionVOS = courseSelectionService.getUsersByCourse(courseId);
        return ServerResponse.success(courseSelectionVOS);
    }

    @PostMapping("/{courseId}")
    public ServerResponse<Void> selectCourse(@PathVariable("courseId") Long courseId) {
        courseSelectionService.selectCourse(courseId);
        return ServerResponse.success();

    }

    @DeleteMapping("/{selectionId}")
    public ServerResponse<Void> cancelSelection(@PathVariable Long selectionId) {
        courseSelectionService.cancelSelection(selectionId);
        return ServerResponse.success();
    }

}
